#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 2e3 + 10;
vector<int>v[N], g[N];
int n, m, d[N], a[N][N];

void dfs(int x) {
	if(x == n + 1) {
		for(int i = 1; i <= n; i++) {
			for(int j = 0; j < v[i].size(); j++) {
				a[d[i]][v[i][j]] = 1;
			}
			for(int j = 0; j < g[i].size(); j++) {
				a[g[i][j]][d[i]] = 1;
			}
		} 
		return;
	}
	for(int i = 1; i <= n; i++) {
		d[x] = i;
		dfs(x + 1);
		d[x] = 0;
	}
}
signed main() {
	freopen("circ.in", "r", stdin);
	freopen("circ.out", "w", stdout);
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int T;
	cin >> T;
	while(T--) {
		
		cin >> n >> m;
		for(int i = 1; i <= n; i++) v[i].clear();
		for(int i = 1; i <= m; i++) {
			int x, y;
			cin >> x >> y;
			v[x].push_back(y);
			g[y].push_back(x);
		}
		cout << n - 1 << '\n';
//		dfs(1);
	}
	return 0;
}

